home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / colrpick / colrpick.sx next >
Encoding:
Text File  |  1996-05-21  |  9.1 KB  |  274 lines  |  [TEXT/ttxt]

  1. in module ColorPickerModule
  2.  
  3. --*******************************************************************************
  4. --*        Class name:    ColorMapShape
  5. --*                                            
  6. --*     Inherits from: TwoDShape                                    
  7. --*        Class type: Concrete
  8. --*         Component: Presenters
  9. --*
  10. --*       Description: This is a subclass of TwoDShape whose boundary is a
  11. --*                    graphical representation of the given colormap. 
  12. --*
  13. --*             Usage: cms := new ColorMapShape colorMap:colorMap \
  14. --*                                             shapeSize:shapeSize
  15. --*
  16. --*               IVs:    none
  17. --*
  18. --*           Methods:    init
  19. --*                    
  20. --*    Required files:    none
  21. --*                    
  22. --*             Notes: 
  23. --*
  24. --*            Author:    Steve Mayer - Kaleida Labs, Inc.
  25. --*******************************************************************************
  26. class ColorMapShape (TwoDShape)
  27. end
  28.  
  29. --*=============================================================================*
  30. --*       Method name:    init
  31. --*             Class:    ColorMapShape
  32. --*             Usage: init self [colormap:<Colormap>]    
  33. --*                              [shapeSize:<Number>]
  34. --*-----------------------------------------------------------------------------*
  35. --*       Description: Creates a TwoDShape whose boundary is a graphical 
  36. --*                    representation of the given colormap. 
  37. --*                    Default values: 
  38. --*                        colormap  - (defaultColormap)
  39. --*                        shapeSize - (10)
  40. --*=============================================================================*
  41. method init self {class ColorMapShape} #rest args \
  42.                                        #key colorMap:(defaultColorMap) \
  43.                                              shapeSize:(10) ->
  44. (
  45.     local b := new BitmapSurface colorMap:colorMap \
  46.                  bBox: (new Rect x2:(16 * (shapeSize - 1)) y2:(16 * (shapeSize - 1)))
  47.     local r := new Rect x2:shapeSize y2:shapeSize
  48.     local c, xOffset, yIndex, yOffset
  49.     local shapeStroke := (new Brush color:(new RGBColor red:50 green:50 blue:50))
  50.     
  51.     for i := 1 to 256 do
  52.     (
  53.         local pc := getNth colorMap i
  54.         if (pc <> empty) do
  55.         (
  56.             c := new Brush color:pc
  57.             xOffset := (shapeSize - 1) * (mod (i - 1) 16)
  58.             yIndex := floor((i - 1) / 16)
  59.             yOffset := (shapeSize - 1) * yIndex
  60.             fill b r b.bBox (translate identityMatrix xOffset yOffset) c
  61.             stroke b r b.bBox (translate identityMatrix xOffset yOffset) shapeStroke
  62.         )
  63.     )
  64.  
  65.     apply nextMethod self boundary:b args
  66.     
  67.     return self
  68. )
  69.  
  70.  
  71.  
  72. --*******************************************************************************
  73. --*        Class name:    ColorPicker
  74. --*                                            
  75. --*     Inherits from: TwoDSpace                                    
  76. --*        Class type: Concrete
  77. --*         Component: Spaces
  78. --*
  79. --*       Description: This is a subclass of TwoDSpace which contains 
  80. --*                    graphical representation of the given colormap. To
  81. --*                    pick a color, just move the mouse over the space and click
  82. --*                    on the desired color.  
  83. --*
  84. --*             Usage: cp := new ColorPicker manager:<some shape>
  85. --*
  86. --*               IVs:    mmev
  87. --*                    mdev
  88. --*                    manager            -- The object to be informed of the color 
  89. --*                                        change.
  90. --*                    colorMap        -- The palette of available colors.
  91. --*                    shapeSize        -- The size of each color rectangle.
  92. --*                    currentColor    -- The currently selected color.
  93. --*                    selection        -- A TwoDShape which overlays the selected 
  94. --*                                        color.
  95. --*
  96. --*           Methods:    getCurrentColor
  97. --*                    mouseMove
  98. --*                    mouseDown
  99. --*                    init
  100. --*                    afterLoading
  101. --*                    
  102. --*    Required files:    none
  103. --*                    
  104. --*             Notes:
  105. --*
  106. --*            Author:    Steve Mayer, Su Quek - Kaleida Labs, Inc.
  107. --*******************************************************************************
  108. class ColorPicker (TwoDSpace)
  109. instance variables
  110.     mmev
  111.     mdev
  112.     manager            
  113.     colorMap        
  114.     shapeSize        
  115.     sampleFill        
  116.     sampleStroke    
  117.     currentColor    
  118.     selection        
  119. end
  120.  
  121.  
  122. --*=============================================================================*
  123. --*       Method name:    getCurrentColor
  124. --*             Class:    ColorPicker
  125. --*             Usage: getCurrentColor self localCoords
  126. --*-----------------------------------------------------------------------------*
  127. --*       Description: Determines the color that is selected and moves the 
  128. --*                    selection box to that location in the space.
  129. --*=============================================================================*
  130. method getCurrentColor self {class ColorPicker} localCoords ->
  131. (
  132.     local shapeSize := self.shapeSize
  133.     local x := ((floor (localCoords.x / (shapeSize - 1)) + 1))
  134.     local y := (floor (localCoords.y / (shapeSize - 1)))
  135.     local i := (y * 16) + x
  136.  
  137.     if (0 < i and i <= 256 and self.currentColor <> i) do
  138.     (
  139.         self.currentColor := i
  140.         self.selection.x := (x - 1) * (shapeSize - 1) + 2
  141.         self.selection.y := y * (shapeSize - 1) + 2
  142.     )
  143.  
  144.     return (new Brush color:(getNth self.colorMap i))
  145. )
  146.  
  147. --*=============================================================================*
  148. --*       Method name:    mouseMove
  149. --*             Class:    ColorPicker
  150. --*             Usage: mouseMove self ev
  151. --*-----------------------------------------------------------------------------*
  152. --*       Description: Highlights the color that the mouse cursor is over.
  153. --*=============================================================================*
  154. method mouseMove self {class ColorPicker} interest ev ->
  155. (
  156.     getCurrentColor self ev.localCoords
  157. )
  158.  
  159. --*=============================================================================*
  160. --*       Method name:    mouseDown
  161. --*             Class:    ColorPicker
  162. --*             Usage: mouseDown self ev
  163. --*-----------------------------------------------------------------------------*
  164. --*       Description: Changes the color of the manager to the color at the 
  165. --*                    location where the mouse is clicked.
  166. --*=============================================================================*
  167. method mouseDown self {class ColorPicker} interest ev ->
  168. (      
  169.     local b := getCurrentColor self ev.localCoords
  170.     local theManager := self.manager
  171.     
  172.     if (theManager <> undefined) do
  173.         colorChanged theManager b
  174. )
  175.  
  176. --*=============================================================================*
  177. --*       Method name:    init
  178. --*             Class:    ColorPicker
  179. --*             Usage: init self [manager:<Object>]
  180. --*                              [colormap:<Colormap>]    
  181. --*                              [shapeSize:<Number>]
  182. --*-----------------------------------------------------------------------------*
  183. --*       Description: Creates a TwoDSpace and initializes its IVs to the default
  184. --*                    values unless another is explicitly given.
  185. --*                    Default values: 
  186. --*                        manager      - (undefined)
  187. --*                        colormap  - (defaultColormap)
  188. --*                        shapeSize - (10)
  189. --*=============================================================================*
  190. method init self {class ColorPicker} #rest args #key manager:(undefined) \
  191.                                                      colorMap:(defaultColorMap) \
  192.                                                      shapeSize:(10) -> 
  193. (
  194.     local r := new Rect x2:(16 * (shapeSize - 1) + 4) \
  195.                         y2:(16 * (shapeSize - 1) + 4)
  196.     apply nextMethod self boundary:r args
  197.     self.fill := whiteBrush
  198.     self.stroke := blackBrush
  199.  
  200.     self.manager := manager
  201.     self.colorMap := colorMap
  202.     self.currentColor := 1
  203.     self.shapeSize := shapeSize
  204.     
  205.     return self
  206. )
  207.  
  208. --*=============================================================================*
  209. --*       Method name:    afterInit
  210. --*             Class:    ColorPicker
  211. --*             Usage: afterInit self [manager:<Object>]
  212. --*                              [colormap:<Colormap>]    
  213. --*                              [shapeSize:<Number>]
  214. --*-----------------------------------------------------------------------------*
  215. --*       Description: Create the selection box and the mouse move and mouse
  216. --*                    down events.
  217. --*=============================================================================*
  218. method afterInit self {class ColorPicker} #rest args #key manager:(undefined) \
  219.                                                      colorMap:(defaultColorMap) \
  220.                                                      shapeSize:(10) -> 
  221. (
  222.     --*=========================================================================*
  223.     --* Create the colormap shape and append it to the space.
  224.     --*=========================================================================*
  225.     local c := new ColorMapShape colorMap:colorMap shapeSize:shapeSize
  226.     c.position := new Point x:2 y:2
  227.     prepend self c
  228.  
  229.     --*=========================================================================*
  230.     --* Create the selection box.
  231.     --*=========================================================================*
  232.     self.selection := new TwoDShape boundary:(new Rect x2:shapeSize y2:shapeSize) \
  233.                               stroke:whiteBrush
  234.     prepend self self.selection
  235.     
  236.     --*=========================================================================*
  237.     --* Create mouse events.
  238.     --*=========================================================================*
  239.     local mDevice := new MouseDevice
  240.     local mmev := new MouseMoveEvent
  241.     mmev.eventReceiver := mouseMove
  242.     mmev.authordata := self
  243.     mmev.device := mDevice
  244.     mmev.presenter := c
  245.     addEventInterest mmev
  246.     self.mmev := mmev
  247.     
  248.     local mdev := new MouseDownEvent
  249.     mdev.eventReceiver := mouseDown
  250.     mdev.authordata := self
  251.     mdev.device := mDevice
  252.     mdev.presenter := c    -- Workaround because closures can't refer to self.
  253.     addEventInterest mdev
  254.     self.mdev := mdev
  255.     
  256.     return self
  257. )
  258.  
  259. --*=============================================================================*
  260. --*       Method name:    afterLoading
  261. --*             Class:    ColorPicker
  262. --*             Usage: afterLoading self str
  263. --*-----------------------------------------------------------------------------*
  264. --*       Description: Load the mouse move and mouse down events.
  265. --*=============================================================================*
  266. method afterLoading self {class ColorPicker} str ->
  267. (
  268.     nextmethod self str
  269.     load self.mmev
  270.     load self.mdev
  271. )
  272.  
  273. "Loaded colrpick.sx"
  274.